import random
import time

from ua_info import  ua_list
from urllib import request

class TiebaSpilder():


    def __init__(self):
        self.url ='https://tieba.baidu.com/f/index/forumpark?cn=&ci=0&pcn=%E4%BD%93%E8%82%B2%E8%BF%B7&pci=275&ct=&st=new&pn={}'


    def get_html(self,url):
        headers = {
            'User-Agent': random.choice(ua_list),
        }
        req = request.Request(headers=headers, url=url)
        res = request.urlopen(req)
        response = res.read().decode('utf-8', 'ignore')
        return response

    def save_html(self,html,filename):
        with open(filename, 'w', encoding='utf-8') as f:
            f.write(html)


    def run(self):
        for page in range(2):
            urlf =  self.url.format(page+1)
            html = self.get_html(urlf)
            filename = "baidu_tieba{}.html".format(page)
            self.save_html(html,filename)
            time.sleep(random.randint(2,3))



if __name__ == '__main__':
    spilder = TiebaSpilder()
    spilder.run();